package com.foreveross.proxyip.application.offer;

import java.util.List;

import org.springframework.web.client.RestTemplate;

import com.foreveross.proxyip.application.offer.bean.ProxyIpDTO;

public class RestProxyIPService implements IProxyIPService {
	RestTemplate restTemplate ;
	
	String restServiceUrl ;
	
	@Override
	public ProxyIpDTO getProxyIp(String auth, String clientIp) throws Exception {
		return restTemplate.getForObject(restServiceUrl+"getProxyIp/{auth}/{clientIp}", ProxyIpDTO.class, auth,clientIp);
	}

	@Override
	public List<ProxyIpDTO> getProxyIps(int size, String auth, String clientIp)
			throws Exception {
		return restTemplate.getForObject(restServiceUrl+"getProxyIps/{size}/{auth}/{clientIp}", List.class,size, auth,clientIp);
	}

	@Override
	public ProxyIpDTO getChannelProxyIp(String channel, String auth,
			String clientIp) throws Exception {
		return restTemplate.getForObject(restServiceUrl+"getChannelProxyIp/{channel}/{auth}/{clientIp}", ProxyIpDTO.class,channel, auth,clientIp);
	}

	@Override
	public List<ProxyIpDTO> getChannelProxyIps(String channel, int size,
			String auth, String clientIp) throws Exception {
		return restTemplate.getForObject(restServiceUrl+"getChannelProxyIps/{channel}/{size}/{auth}/{clientIp}", List.class,channel,size, auth,clientIp);
	}

	@Override
	public boolean recycle(ProxyIpDTO proxyIp, String auth) throws Exception {
		if(proxyIp==null){
			throw new Exception("ipBean 为空");
		}
		if(proxyIp.getChannelId()==null||"".equals(proxyIp.getChannelId())){
			throw new Exception("channId  为空,请检查！！！！");
		}
		return restTemplate.postForObject(restServiceUrl+"recycle/{auth}", proxyIp,  Boolean.class, auth);
	}

	public RestTemplate getRestTemplate() {
		return restTemplate;
	}

	public void setRestTemplate(RestTemplate restTemplate) {
		this.restTemplate = restTemplate;
	}

	public String getRestServiceUrl() {
		return restServiceUrl;
	}

	public void setRestServiceUrl(String restServiceUrl) {
		this.restServiceUrl = restServiceUrl + "/cxf/proxyipService/";
	}
}
